package com.hevery.web.config;

import com.hevery.web.filter.TimeFilter;
import com.hevery.web.interceptor.TimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Hevery
 * @Description: 在SpringBoot配置第三方框架的(此处为了演示，使用了自定义的过滤器)过滤器
 * @Date: Created on 2017/10/7
 * @Modified By:
 */
//@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
    @Autowired
    private TimeInterceptor timeInterceptor;


    /**
     * 配置支持异步请求的方法
     * */
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        /**注册异步情况下的拦截器，拦截异步请求*/
        configurer.registerCallableInterceptors();
        configurer.registerDeferredResultInterceptors();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(timeInterceptor);
    }

    @Bean
    public FilterRegistrationBean timeFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        TimeFilter timeFilter = new TimeFilter();
        registrationBean.setFilter(timeFilter);
        List<String> urls = new ArrayList<>();
        /**设置对所有的url都起作用*/
        urls.add("/*");
        registrationBean.setUrlPatterns(urls);
        return registrationBean;
    }
    /*@Bean
    public FilterRegistrationBean responseFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        ResponseFilter responseFilter = new ResponseFilter();
        registrationBean.setFilter(responseFilter);
        List<String> urls = new ArrayList<>();
        *//**设置对所有的url都起作用*//*
        urls.add("*//*");
        registrationBean.setUrlPatterns(urls);
        return registrationBean;
    }*/
    /* @Bean
    public FilterRegistrationBean requestFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        RequestFilter requestFilter = new RequestFilter();
        registrationBean.setFilter(requestFilter);
        List<String> urls = new ArrayList<>();

        urls.add("");
        registrationBean.setUrlPatterns(urls);
        return registrationBean;
    }*/
}
